package action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections4.map.HashedMap;

import com.google.gson.Gson;

import pojo.BaseDataPojo;
import pojo.Book;
import pojo.BookImage;
import pojo.MallHomeImage;
import pojo.Resource;
import pojo.ResourceComment;
import pojo.ShoppingCart;
import service.ForumService;
import service.MallService;
import serviceImpl.ForumServiceImpl;
import serviceImpl.MallServiceImpl;

/**
 * Servlet implementation class MallServlet
 */
@WebServlet("/MallServlet")
public class MallServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
    public MallServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doPost(request, response);
		
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String mark = request.getParameter("mark");
		switch(mark) {
		
		case "getHomeIma":   // 获取商城首页轮播图图片
			getHomeIma(request,response);
			break;
		case "getBooks":   // 获取商城首页轮播图图片
			getBooks(request,response);
			break;
		case "getBookImages":   // 获取商城首页轮播图图片
			getBookImages(request,response);
			break;
		case "getCartBook":   // 获取商城首页轮播图图片
			getCartBook(request,response);
			break;
		case "addCart":   // 获取商城首页轮播图图片
			addCart(request,response);
			break;
		case "carRemove":   // 获取商城首页轮播图图片
			carRemove(request,response);
			break;
		case "toPay":   // 获取商城首页轮播图图片
			toPay(request,response);
			break;
		case "emptyCart":   // 获取商城首页轮播图图片
			emptyCart(request,response);
			break;
		case "searchBook":   // 获取商城首页轮播图图片
			searchBook(request,response);
			break;
			
		}
		
	}

	private void searchBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		
		String book =request.getParameter("book");
		MallService ms = new MallServiceImpl();
		ArrayList<Book> books = new ArrayList<Book>();
		books = ms.queryBookByName(book);
		Map<String,ArrayList<Book>> map = new HashedMap<>();
		map.put("books",books);
		System.out.println("books:"+books);
		if(books!=null) {
			out.print(new Gson().toJson(new BaseDataPojo<Map<String, 
					ArrayList<Book>>>("查询资料成功", true, map)));
		}else {
			out.print(new Gson().toJson(new BaseDataPojo<Map<String, 
					ArrayList<Book>>>("查询资料为空或失败", false, map)));
		}
	}

	private void emptyCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		int userid = Integer.parseInt(request.getParameter("userid"));
		
		MallService ms = new MallServiceImpl();
		int suc = 0;
		suc = ms.emptyCart(userid);
		Map<String,Integer> map = new HashedMap<>();
		map.put("suc",suc);
		System.out.println("suc:"+suc);
		if(suc==1||suc==0) {
			out.print(new Gson().toJson(new BaseDataPojo<Map<String, 
					Integer>>("成功", true, map)));
		}else {
			out.print(new Gson().toJson(new BaseDataPojo<Map<String, 
					Integer>>("为空或失败", false, map)));
		}
		
	}

	private void toPay(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		int userid = Integer.parseInt(request.getParameter("userid"));
		float money = Float.parseFloat(request.getParameter("userMoney"));
		
		MallService ms = new MallServiceImpl();
		int suc = 0;
		suc = ms.toPay(userid,money);
		Map<String,Integer> map = new HashedMap<>();
		map.put("suc",suc);
		System.out.println("suc:"+suc);
		if(suc==1||suc==0) {
			out.print(new Gson().toJson(new BaseDataPojo<Map<String, 
					Integer>>("成功", true, map)));
		}else {
			out.print(new Gson().toJson(new BaseDataPojo<Map<String, 
					Integer>>("为空或失败", false, map)));
		}
		
	}

	private void carRemove(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		int scid = Integer.parseInt(request.getParameter("scid"));
		
		MallService ms = new MallServiceImpl();
		int suc = 0;
		suc = ms.carRemove(scid);
		Map<String,Integer> map = new HashedMap<>();
		map.put("suc",suc);
		System.out.println("suc:"+suc);
		if(suc==1||suc==0) {
			out.print(new Gson().toJson(new BaseDataPojo<Map<String, 
					Integer>>("成功", true, map)));
		}else {
			out.print(new Gson().toJson(new BaseDataPojo<Map<String, 
					Integer>>("为空或失败", false, map)));
		}
		
	}

	private void addCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		int userid = Integer.parseInt(request.getParameter("userid"));
		int bookid = Integer.parseInt(request.getParameter("bookid"));
		
		MallService ms = new MallServiceImpl();
		int suc = 0;
		suc = ms.getCartBook(userid,bookid);
		Map<String,Integer> map = new HashedMap<>();
		map.put("suc",suc);
		System.out.println("suc:"+suc);
		if(suc==1||suc==0) {
			out.print(new Gson().toJson(new BaseDataPojo<Map<String, 
					Integer>>("成功", true, map)));
		}else {
			out.print(new Gson().toJson(new BaseDataPojo<Map<String, 
					Integer>>("为空或失败", false, map)));
		}
		
	}

	private void getCartBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		int userid = Integer.parseInt(request.getParameter("userid"));
		
		MallService ms = new MallServiceImpl();
		ArrayList<ShoppingCart> cartList = new ArrayList<ShoppingCart>();
		cartList = ms.getCartBook(userid);
		Map<String,ArrayList<ShoppingCart>> map = new HashedMap<>();
		map.put("cartList",cartList);
		//System.out.println("bImaList:"+bImaList);
		if(cartList!=null) {
			out.print(new Gson().toJson(new BaseDataPojo<Map<String, 
					ArrayList<ShoppingCart>>>("根据userid获取book成功", true, map)));
		}else {
			out.print(new Gson().toJson(new BaseDataPojo<Map<String, 
					ArrayList<ShoppingCart>>>("根据userid获取book为空或失败", false, map)));
		}
		
	}

	private void getBookImages(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		int bookid = Integer.parseInt(request.getParameter("bookid"));
		//System.out.println("bookid"+bookid);
	
		MallService ms = new MallServiceImpl();
		ArrayList<BookImage> bImaList = new ArrayList<BookImage>();
		bImaList = ms.getBookImages(bookid);
		Map<String,ArrayList<BookImage>> map = new HashedMap<>();
		map.put("bImaList",bImaList);
		//System.out.println("bImaList:"+bImaList);
		if(bImaList!=null) {
			out.print(new Gson().toJson(new BaseDataPojo<Map<String, 
					ArrayList<BookImage>>>("根据bookid获取bookImage成功", true, map)));
		}else {
			out.print(new Gson().toJson(new BaseDataPojo<Map<String, 
					ArrayList<BookImage>>>("根据bookid获取bookImage为空或失败", false, map)));
		}
		
	}

	private void getBooks(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
	
		MallService ms = new MallServiceImpl();
		ArrayList<Book> bList = new ArrayList<Book>();
		bList = ms.getBooks();
		Map<String,ArrayList<Book>> map = new HashedMap<>();
		map.put("bList",bList);
		//System.out.println("bList:"+bList);
		if(bList!=null) {
			out.print(new Gson().toJson(new BaseDataPojo<Map<String, 
					ArrayList<Book>>>("根据mid获取单个Message成功", true, map)));
		}else {
			out.print(new Gson().toJson(new BaseDataPojo<Map<String, 
					ArrayList<Book>>>("根据mid获取单个Message为空或失败", false, map)));
		}
		
	}

	private void getHomeIma(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
	
		MallService ms = new MallServiceImpl();
		ArrayList<MallHomeImage> mhiList = new ArrayList<MallHomeImage>();
		mhiList = ms.getHomeIma();
		Map<String,ArrayList<MallHomeImage>> map = new HashedMap<>();
		map.put("mhiList",mhiList);
		//System.out.println("mhiList:"+mhiList);
		if(mhiList!=null) {
			out.print(new Gson().toJson(new BaseDataPojo<Map<String, 
					ArrayList<MallHomeImage>>>("根据mid获取单个Message成功", true, map)));
		}else {
			out.print(new Gson().toJson(new BaseDataPojo<Map<String, 
					ArrayList<MallHomeImage>>>("根据mid获取单个Message为空或失败", false, map)));
		}
		
	}

}
